package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkQualityTemplateRecord;
import com.ysd.lis.mapper.WkQualityTemplateRecordMapper;
import com.ysd.lis.service.WkQualityTemplateRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 模板修改记录
 *
 * @author LuoXS
 * @date 2024/5/13
 * version 1.0
 */
@Service
public class WkQualityTemplateRecordServiceImpl extends ServiceImpl<WkQualityTemplateRecordMapper, WkQualityTemplateRecord> implements WkQualityTemplateRecordService {

    @Resource
    WkQualityTemplateRecordMapper mapper;


    @Override
    public Result add(WkQualityTemplateRecord params) {
        Result result = Result.succ(1, "添加成功！", null);
        try {
            int insert = mapper.insert(params);
            if (insert < 0) {
                result = Result.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "添加失败！", e.getMessage());
        }
        return result;
    }

    @Override
    public Result pageList(WkQualityTemplateRecord pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<WkQualityTemplateRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WkQualityTemplateRecord::getDelFlag, 0);
            lambdaQueryWrapper.eq(WkQualityTemplateRecord::getSourceId, pageParam.getId());
            lambdaQueryWrapper.eq(StringUtils.isNotBlank(pageParam.getSourceType()), WkQualityTemplateRecord::getSourceType, pageParam.getSourceType());
            lambdaQueryWrapper.orderByDesc(WkQualityTemplateRecord::getCreateTime);
            List<WkQualityTemplateRecord> wkCalcFormulas = mapper.selectList(lambdaQueryWrapper);
            result = Result.succ(wkCalcFormulas);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "查询失败！", e.getMessage());
        }
        return result;
    }

}
